package com.cqs.leetcode.array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * lixw
 * 2022/10/25
 **/
public class PartitionArrayDisjointIntervals915 {

    //1,1,1,1,1
    //5, 0, 3,3, 8, 6
    public int partitionDisjoint(int[] nums) {
        int[] leftMax = new int[nums.length];
        int max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            leftMax[i] = max = Math.max(nums[i], max);
        }
        int[] rightMin = new int[nums.length];
        int min = nums[nums.length - 1];
        for (int i = nums.length - 1; i >= 0; i--) {
            rightMin[i] = min = Math.min(min, nums[i]);
        }
        int idx = 0;
        while ((idx < nums.length - 1) && leftMax[idx] > rightMin[idx + 1]) {
            idx++;
        }

        return 1 + idx;
    }

    public static void main(String[] args) {
        PartitionArrayDisjointIntervals915 p = new PartitionArrayDisjointIntervals915();
        List<int[]> list = new ArrayList<>();
        list.add(new int[]{5, 0, 3, 8, 6});
        list.add(new int[]{1,1,1,0,6,12});
        for (int[] ints : list) {
            int i = p.partitionDisjoint(ints);
            List<Integer> collect = Arrays.stream(ints).boxed().collect(Collectors.toList());
            System.out.println(collect + "\t" + i);
        }
    }

}
